import e from "jquery";
import t from "./view_container";
import n from "./assets_loader";
import r from "./event_center";
import i from "./event";
import s from "./progress_event";
import o from "./error_event";

export default class ViewCtrl {
  constructor(e?) {
    typeof e === "object" && this.initWithView(e);
  }
  // TODO: u.prototype.constructor = u
  inited = !1;
  prepared = !1;
  _view = null;
  view(e?) {
    let t = this;
    return (
      arguments.length > 0 &&
        e != t._view &&
        (t._view != null && (t._view.controller = null),
        (t._view = e),
        (t._view.controller = t)),
      t._view == null &&
        ((t._view = t.generateView()),
        (t._view.controller = t),
        t.$viewXML != null &&
          t.prepared &&
          t._view.updatePropsFromXML(t.$viewXML)),
      t._view
    );
  }
  generateView() {
    return new t();
  }
  _loadPercent = 0;
  loadPercent() {
    let e = this;
    return e._loadPercent;
  }
  init() {
    let e = this;
    if (e.inited !== !1) throw new Error("View controller has init already.");
    (e.inited = !0),
      (e.prepared = !0),
      (e._loadPercent = 1),
      e.onViewControllerPrepared();
  }
  initWithView(e) {
    let t = this;
    // TODO: 结构变了
    console.log('initWithView', t);
    if (t.inited !== !1) throw new Error("View controller has init already.");
    t.view(e), t.init();
  }
  $viewXML = null;
  afterPrepared() {
    let e = this;
    (e.inited = !0), (e.prepared = !0), (e._loadPercent = 1);
    if (e._view != null) {
      e._view.updatePropsFromXML(e.$viewXML), e.onViewControllerPrepared();
      if (e._view.onStage) {
        let t = e._view.stage();
        e._view.resize(t.stageWidth(), t.stageHeight()),
          e._view.onWindowOrientationChanged(t.orientation);
      }
    } else e.onViewControllerPrepared();
    r.dispatchEvent(new i(e, i.PREPARED));
  }
  initWithXML(t) {
    let n = this;
    if (n.inited !== !1) throw new Error("View controller has init already.");
    (n._loadPercent = 0), (n.inited = !0), (n.prepared = !1), (n.$viewXML = t);
    let r = n.$viewXML.find("asset");
    if (r.length <= 0) n.afterPrepared();
    else {
      let i = [];
      r.each(function (t, n) {
        i.push(e(n).text());
      }),
        n.loadAssets(i);
    }
  }
  initWithURL(t) {
    let s = this;
    if (s.inited !== !1) throw new Error("View controller has init already.");
    console.log('initWithURL', t)
    let u = n.asset(t);
    console.log('asset', u)
    if (u != null) s.initWithXML(u);
    else {
      (s._loadPercent = 0), (s.inited = !0), (s.prepared = !1);
      let a = new n();
      r.addListener(
        a,
        s,
        [i.LOAD, o.ERROR],
        [
          function () {
            s.$viewXML = n.asset(t);
            let i = s.$viewXML.find("asset");
            if (i.length <= 0) s.afterPrepared();
            else {
              let o = [];
              i.each(function (t, n) {
                o.push(e(n).text());
              }),
                s.loadAssets(o);
            }
            r.removeListener(a, s);
          },
          function (e) {
            throw (r.removeListener(a, s), e.error);
          },
        ]
      ),
        a.loadAssets(t);
    }
  }
  assets = null;
  loadAssets(e) {
    let t = this;
    t.assets = e;
    let u = new n();
    r.addListener(
      u,
      t,
      [i.LOAD, s.PROGRESS, o.ERROR],
      [
        function () {
          t.afterPrepared(), r.removeListener(u, t);
        },
        function (e) {
          (t._loadPercent = e.percent),
            t.onViewControllerProgress(t._loadPercent),
            r.dispatchEvent(e.clone(t));
        },
        function (e) {
          throw (r.removeListener(u, t), e.error);
        },
      ]
    ),
      u.loadAssets(e);
  }
  dealloc() {
    let e = this;
    e._view != null && (e._view.dealloc(), (e._view = null)),
      (e.inited = !1),
      e.assets != null && (n.releaseAssets(e.assets), (e.assets = null)),
      r.removeListener(e);
  }
  onViewUpdateFromXML(e) {}
  onViewControllerPrepared() {}
  onViewControllerProgress(e) {}
  onViewAddToStage() {}
  onViewAdded(e) {}
  onViewRemoved(e) {}
  onViewRemoveFromStage() {}
  onViewStartAnimate() {}
  onViewEndAnimate() {}
  onViewUpdated(e, t) {}
  onViewDeactivate() {}
  onViewActivate() {}
  onViewResize(e, t) {}
  onWindowOrientationChanged(e) {}
}
